import { Boards } from 'mixly';

//--------------ASBIT/ESP32部分板载库------------
export const serial_xxbit_image_any = function (_, generator) {
    var version = Boards.getSelectedBoardKey().split(':')[2];
    generator.definitions_['from ' + version + ' import *'] = 'from ' + version + ' import *';
    var code = 'uart1.any()';
    return [code, generator.ORDER_ATOMIC];
}

export const serial_xxbit_image_recv = function (_, generator) {
    var version = Boards.getSelectedBoardKey().split(':')[2];
    generator.definitions_['from ' + version + ' import *'] = 'from ' + version + ' import *';
    var code = 'uart1.read()';
    return [code, generator.ORDER_ATOMIC];
}

export const serial_xxbit_send = function (_, generator) {
    var version = Boards.getSelectedBoardKey().split(':')[2];
    generator.definitions_['from ' + version + ' import *'] = 'from ' + version + ' import *';
    var VAR = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC);
    var stat = this.getFieldValue('STAT');
    var code = 'ob_send(' + VAR + ',' + stat + ')\n';
    return code;
}

export const serial_xxbit_recv = function (_, generator) {
    var version = Boards.getSelectedBoardKey().split(':')[2];
    generator.definitions_['from ' + version + ' import *'] = 'from ' + version + ' import *';
    var code = 'ob_recv()';
    return [code, generator.ORDER_ATOMIC];
}

//--------------ACBIT板载库------------
export const sensor_acbit_button_is = function (_, generator) {
    generator.definitions_['from acbit import *'] = 'from acbit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.is_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_acbit_button_was = function (_, generator) {
    generator.definitions_['from acbit import *'] = 'from acbit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.was_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

export const serial_asbit_motor = function (_, generator) {
    generator.definitions_['from acbit import *'] = 'from acbit import *';
    var speed = generator.valueToCode(this, 'speed', generator.ORDER_ATOMIC);
    var stat = this.getFieldValue('STAT');
    var code = '' + stat + '.run(' + speed + ')\n';
    return code;
}

export const serial_acbit_led = function (_, generator) {
    generator.definitions_['from acbit import *'] = 'from acbit import *';
    var value = generator.valueToCode(this, 'value', generator.ORDER_ATOMIC);
    var code = 'ob_led.setbrightness(' + value + ')\n';
    return code;
}

export const sensor_acbit_get_brightness = function (_, generator) {
    generator.definitions_['from acbit import *'] = 'from acbit import *';
    var code = 'get_brightness()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_xxbit_get_vbat = function (_, generator) {
    var version = Boards.getSelectedBoardKey().split(':')[2];
    generator.definitions_['from ' + version + ' import *'] = 'from ' + version + ' import *';
    var code = 'get_vbat()';
    return [code, generator.ORDER_ATOMIC];
}

//--------------LTBIT板载库------------
export const sensor_ltbit_button_is = function (_, generator) {
    generator.definitions_['from acbit import *'] = 'from ltbit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.is_pressed()';
    return [code, generator.ORDER_ATOMIC];
}

export const sensor_ltbit_button_was = function (_, generator) {
    generator.definitions_['from ltbit import *'] = 'from ltbit import *';
    var key = this.getFieldValue('TYPE');
    var code = '' + key + '.was_pressed()';
    return [code, generator.ORDER_ATOMIC];
}